package cn.hibernatedemo.dao;

import cn.hibernatedemo.entity.Dept;
import cn.hibernatedemo.entity.Emp;
import cn.hibernatedemo.entity.EmpCondition;

import java.util.List;

public class EmpDao {
    public List<Emp> getEmpsByConditions(String hql,EmpCondition empCondition){
        return HibernateUtil.currentSession().createQuery(hql).setProperties(empCondition).list();
    }
    public Long getEmpsByConditions(Float sal){
        String hql="select count(empNo) from Emp where sal>=:sal";
        return (Long)HibernateUtil.currentSession().createQuery(hql).setFloat("sal",sal).uniqueResult();
    }

    public List<Emp> getEmpPage(Integer pageIndex,Integer pageSize){
        String hql="from Emp";
        return HibernateUtil.currentSession().createQuery(hql).setFirstResult((pageIndex-1)*pageSize)
                .setMaxResults(pageSize).list();
    }
    public List<String> getdnames(){
        String hql="select empName from Emp";
        List<String> strs=HibernateUtil.currentSession().createQuery(hql).list();
        return strs;
    }
    public List<Object[]> getObject(){
        String hql="select empName,job from Emp";
        List<Object[]> strs=HibernateUtil.currentSession().createQuery(hql).list();
        return strs;
    }
    public List<Emp> getEmps(){
        String hql="select new Emp(empName,job) from Emp";
        List<Emp> strs=HibernateUtil.currentSession().createQuery(hql).list();
        return strs;
    }
    public void addEmp(Emp emp){
        HibernateUtil.currentSession().save(emp);
    }

    public List<Emp> findByDept(Dept dept){
        String hql="from Emp where dept=?";
        return HibernateUtil.currentSession().createQuery(hql).setParameter(0,dept).list();
    }
    public List<Emp> findEmps( ){
        String hql="from Emp";
        return HibernateUtil.currentSession().createQuery(hql).list();
    }

    public Emp load(Integer empNo){
        return (Emp)HibernateUtil.currentSession().load(Emp.class,empNo);

    }


}
